#!/bin/bash

env_name=$1
if [ -z ${env_name} ]; then
	echo "Usage: bash create_env.sh <env_name>"
	echo "[ERROR] Must Specify the name of environment."
	exit 1
fi
grep_name=`conda env list | cut -d' ' -f1 | grep "^${env_name}$"`
if [[ ${env_name} = ${grep_name} ]]; then
	echo "Virtual environment already existed: ${env_name}"
	exit 0
fi


set -e
huawei_pypi="https://repo.huaweicloud.com/repository/pypi/simple"


#>>>>>>>>>>>>>>>>>>>>> 安装基础环境 >>>>>>>>>>>>>>>>>>>>>>>
conda create -n ${env_name} python=3.7.5
source activate ${env_name}
pip install torch==1.8.0 -i ${huawei_pypi}
pip install scipy==1.7.3 -i ${huawei_pypi}
pip install boto3==1.23.10 -i ${huawei_pypi}
pip install psutil==5.9.4 -i ${huawei_pypi}
pip install absl-py==1.2.0 -i ${huawei_pypi}
pip install tensorflow==2.11.0 -i ${huawei_pypi}
pip install onnx==1.12.0 -i ${huawei_pypi}
pip install onnx-simplifier==0.4.17 -i ${huawei_pypi}
pip install decorator==5.1.1 -i ${huawei_pypi}
pip install attrs==22.2.0 -i ${huawei_pypi}
pip install tqdm==4.63.1 -i ${huawei_pypi}


#>>>>>>>>>>>>>>>>>>>>>>> 安装改图工具 >>>>>>>>>>>>>>>>>>>>>>>>
git clone https://gitee.com/Ronnie_zheng/MagicONNX.git MagicONNX
cd MagicONNX && git checkout 45f4fd50a7e7ca76ee90365fec1a907b25ffa8e8
pip3 install . -i ${huawei_pypi} && cd ..

git clone https://gitee.com/ascend/msadvisor.git
cd msadvisor/auto-optimizer
pip install --upgrade pip -i ${huawei_pypi}
pip install wheel -i ${huawei_pypi}
pip install . -i ${huawei_pypi}
cd ../../


#>>>>>>>>>>>>>>>>>>>>>>> 安装量化工具 >>>>>>>>>>>>>>>>>>>>>>>
wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%206.3.RC1/Ascend-cann-amct_6.3.RC1_linux-x86_64.zip
unzip Ascend-cann-amct_6.3.RC1_linux-x86_64.zip -d amct
cd amct/
tar -zxvf Ascend-cann-amct_6.3.RC1_linux-x86_64.tar.gz
cd amct/amct_onnx/
pip install amct_onnx-0.8.2-py3-none-linux_x86_64.whl
tar -zxvf amct_onnx_op.tar.gz
pip install onnxruntime==1.8.0 -i ${huawei_pypi}
git clone -b v1.8.0 https://github.com/microsoft/onnxruntime.git
cd onnxruntime/include/onnxruntime/core/session/
cp onnxruntime_c_api.h ../../../../../amct_onnx_op/inc/
cp onnxruntime_cxx_api.h ../../../../../amct_onnx_op/inc/
cp onnxruntime_cxx_inline.h ../../../../../amct_onnx_op/inc/
cp onnxruntime_session_options_config_keys.h ../../../../../amct_onnx_op/inc/
cd ../../../../../amct_onnx_op/
python setup.py build
cd ../../../../


#>>>>>>>>>>>>>>>>>>>>>>> 安装推理工具 >>>>>>>>>>>>>>>>>>>>>>>
arch=`arch`
if [[ $arch = "x86_64" ]]; then
	wget https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/aclruntime-0.0.2-cp37-cp37m-linux_x86_64.whl
	pip install aclruntime-0.0.2-cp37-cp37m-linux_x86_64.whl
	rm aclruntime-0.0.2-cp37-cp37m-linux_x86_64.whl
elif [[ $arch = "aarch64" ]]; then
	wget https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/aclruntime-0.0.2-cp37-cp37m-linux_aarch64.whl
	pip install aclruntime-0.0.2-cp37-cp37m-linux_aarch64.whl
	rm aclruntime-0.0.2-cp37-cp37m-linux_aarch64.whl
else
	echo "aclruntime not supported the architecture: ${arch}"
	exit 1
fi
wget https://aisbench.obs.myhuaweicloud.com/packet/ais_bench_infer/0.0.2/ais_bench-0.0.2-py3-none-any.whl
pip install ais_bench-0.0.2-py3-none-any.whl
rm ais_bench-0.0.2-py3-none-any.whl


#>>>>>>>>>>>>>>>>>>>>>>> 获取前后处理代码 >>>>>>>>>>>>>>>>>>>>>>>
git clone https://gitee.com/ascend/ModelZoo-PyTorch.git
cp -r ModelZoo-PyTorch/ACL_PyTorch/built-in/nlp/Bert_Base_Uncased_for_Pytorch/ .
rm -rf ModelZoo-PyTorch
cd Bert_Base_Uncased_for_Pytorch/


#>>>>>>>>>>>>>>>>>>>>>>> 获取Bert源码 >>>>>>>>>>>>>>>>>>>>>>>
git clone https://github.com/NVIDIA/DeepLearningExamples.git        
cd ./DeepLearningExamples             
git reset --hard dd6b8ca2bb80e17b015c0f61e71c2a84733a5b32
cd ..
